package ted

import (
	"Readme/inits"
	"Readme/model"
)

func LoginMysql(mobile string, password string) (user model.User, err error) {
	err = inits.DB.Where("mobile=?", mobile).Where("password=?", password).Find(&user).Error
	if err != nil {
		return model.User{}, err
	}
	return user, nil

}

func Register(mobile string, password string) (user model.User, err error) {
	user = model.User{
		Mobile:   mobile,
		Password: password,
	}
	err = inits.DB.Create(&user).Error
	if err != nil {
		return model.User{}, err
	}
	return user, nil
}

func Conts(userId int, name string, neiroh string) (cont model.Cont, err error) {
	cont = model.Cont{
		UserId: userId,
		Name:   name,
		Neiroh: neiroh,
	}
	err = inits.DB.Create(&cont).Error
	if err != nil {
		return model.Cont{}, err
	}
	return cont, nil
}

func Dianzan() (cont []model.Cont, err error) {
	err = inits.DB.Find(&cont).Error
	if err != nil {
		return []model.Cont{}, err
	}
	return cont, nil
}

func Guanzhu(id uint, id2 int) (guznhu model.Cont, err error) {
	guznhu = model.Cont{
		UserId:  int(id),
		Dianzan: id2,
	}
	err = inits.DB.Create(&guznhu).Error
	if err != nil {
		return model.Cont{}, err
	}
	return guznhu, nil
}

func PingLun(id uint, id2 int, id3 int, content string) (pinglun model.Pinlun, err error) {
	pinglun = model.Pinlun{
		UserId:   int(id),
		PinlunId: id2,
		FandId:   id3,
		Content:  content,
	}
	err = inits.DB.Create(&pinglun).Error
	if err != nil {
		return model.Pinlun{}, err
	}
	return pinglun, nil
}
